package com.xpengplayer.update;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.core.app.NotificationCompat;

/**
 * 下载进度通知管理器
 * 用于显示APK下载过程中的进度通知
 */
public class DownloadNotificationManager {
    private static final String CHANNEL_ID = "update_notification_channel";
    private static final String CHANNEL_NAME = "应用更新通知";
    private static final String CHANNEL_DESCRIPTION = "显示应用更新的下载进度";
    private static final int NOTIFICATION_ID = 1001;
    
    private NotificationManager notificationManager;
    private NotificationCompat.Builder builder;
    private Context context;
    
    public DownloadNotificationManager(Context context) {
        this.context = context;
        initNotificationChannel();
        initNotificationBuilder();
    }
    
    /**
     * 初始化通知渠道
     */
    private void initNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager = context.getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                NotificationChannel channel = new NotificationChannel(
                        CHANNEL_ID,
                        CHANNEL_NAME,
                        NotificationManager.IMPORTANCE_DEFAULT
                );
                channel.setDescription(CHANNEL_DESCRIPTION);
                notificationManager.createNotificationChannel(channel);
            }
        } else {
            notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
    }
    
    /**
     * 初始化通知构建器
     */
    private void initNotificationBuilder() {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                0,
                intent,
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_UPDATE_CURRENT : 0
        );
        
        builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(android.R.drawable.ic_menu_info_details)
                .setContentTitle("应用更新")
                .setContentText("正在检查更新...")
                .setContentIntent(pendingIntent)
                .setOngoing(true)
                .setProgress(100, 0, false);
    }
    
    /**
     * 显示下载进度通知
     */
    public void showDownloadProgress(int progress, String fileName, long totalSize, long downloadedSize) {
        if (builder != null) {
            String progressText = String.format("正在下载更新: %s (%d%%)", 
                    formatFileSize(downloadedSize) + "/" + formatFileSize(totalSize), progress);
            
            builder.setContentTitle("正在下载更新")
                    .setContentText(progressText)
                    .setProgress(100, progress, false);
            
            Notification notification = builder.build();
            notificationManager.notify(NOTIFICATION_ID, notification);
        }
    }
    
    /**
     * 显示下载完成通知
     */
    public void showDownloadComplete(String fileName) {
        if (builder != null) {
            builder.setContentTitle("下载完成")
                    .setContentText("点击安装更新")
                    .setProgress(0, 0, false)
                    .setOngoing(false);
            
            Notification notification = builder.build();
            notificationManager.notify(NOTIFICATION_ID, notification);
        }
    }
    
    /**
     * 显示下载失败通知
     */
    public void showDownloadFailed(String errorMessage) {
        if (builder != null) {
            builder.setContentTitle("下载失败")
                    .setContentText(errorMessage)
                    .setProgress(0, 0, false)
                    .setOngoing(false);
            
            Notification notification = builder.build();
            notificationManager.notify(NOTIFICATION_ID, notification);
        }
    }
    
    /**
     * 取消通知
     */
    public void cancelNotification() {
        if (notificationManager != null) {
            notificationManager.cancel(NOTIFICATION_ID);
        }
    }
    
    /**
     * 格式化文件大小
     */
    private String formatFileSize(long size) {
        if (size <= 0) return "0 B";
        final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return String.format("%.2f %s", size / Math.pow(1024, digitGroups), units[digitGroups]);
    }
}